<template>
    <div>
        <el-dialog
            :title="!form.id ? '新增' : '修改'"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">
                <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                    <el-form-item label="等级名称" prop="name">
                        <el-input placeholder="" v-model="form.name" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="级别" prop="level">
                        <el-radio-group v-model="form.level">
                            <el-radio :label="1">一级</el-radio>
                            <el-radio :label="2">二级</el-radio>
                            <el-radio :label="3">三级</el-radio>
                            <el-radio :label="4">四级</el-radio>
                            <el-radio :label="8">五级</el-radio>
                            <el-radio :label="9">六级</el-radio>
                        </el-radio-group>
                    </el-form-item> -->

                    <el-form-item label="等级图标" prop="img">
                        <SingleUpload v-model="form.img" listType="picture-card"></SingleUpload>
                    </el-form-item>
                    <el-form-item label="默认等级" prop="defaultStatus">
                        <el-radio-group v-model="form.defaultStatus">
                            <el-radio :label="1">是</el-radio>
                            <el-radio :label="2">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="注册费用" prop="fee">
                        <el-input v-model="form.fee" placeholder="" type="number" @input="numberic(form,'rebate',$event)" style="width:200px"></el-input>
                    </el-form-item>
                    <el-form-item label="折扣" prop="rebate">
                        <el-input v-model="form.rebate" placeholder="1~100" type="number" @input="numberic100(form,'rebate',$event)" style="width:200px"></el-input>
                    </el-form-item>
                    <el-form-item label="返点" prop="commision">
                        <el-input v-model="form.commision" placeholder="1~100" type="number" @input="numberic100(form,'commision',$event)" style="width:200px"></el-input>
                    </el-form-item>
                    <el-form-item label="会员费提成" prop="deduct">
                        <el-input v-model="form.deduct" placeholder="1~100" type="number" @input="numberic100(form,'deduct',$event)" style="width:200px"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="会员费提成类型" prop="deductType">
                        <el-radio-group v-model="form.deductType">
                            <el-radio :label="0">无会员提成</el-radio>
                            <el-radio :label="1">首次</el-radio>
                            <el-radio :label="2">每次</el-radio>
                        </el-radio-group>
                    </el-form-item> -->
                    <el-form-item label="分销提成" prop="distributionCommission">
                        <el-input v-model="form.distributionCommission" placeholder="1~100" type="number" @input="numberic100(form,'distributionCommission',$event)" style="width:200px"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="分销提成类型" prop="distributionCommissionType">
                        <el-radio-group v-model="form.distributionCommissionType">
                            <el-radio :label="0">无分销提成</el-radio>
                            <el-radio :label="1">首单</el-radio>
                            <el-radio :label="2">每单</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="运费类型" prop="freightType">
                        <el-radio-group v-model="form.freightType">
                            <el-radio :label="1">运费自付</el-radio>
                            <el-radio :label="2">低于5000，运费自付</el-radio>
                            <el-radio :label="3">包运费</el-radio>
                        </el-radio-group>
                    </el-form-item> -->
                    <el-form-item label="备注" prop="remarks">
                        <el-input v-model="form.remarks" type="textarea" :rows="4" placeholder="请输入内容" style="width: 80%;"></el-input>
                    </el-form-item>
                </el-form>
            </div>


            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
import SingleUpload from '@/views/components/upload/single-upload'
import { numberic, numberic100 } from "@/libs";
export default {
    components: {
        SingleUpload
    },
    data() {
        return {
            visible: false,
            submitLoading: false,

            form: {
                name: null,
                img: null,
                level: null,
                fee: null,
                rebate: null,
                commision: null,
                deduct: null,
                deductType: null,
                distributionCommission: null,
                distributionCommissionType: null,
                freightType: null,
                defaultStatus: null,
                remarks: null,
                ctime: null,
                utime: null
            },
            rules: {
                name: [
                    { required: true, message: '等级名称不能为空', trigger: 'blur' }
                ]
            },

            numberic,
            numberic100
        }
    },

    methods: {
        init(id) {
            const refs = this.$refs
            // 情况表单数据
            this.form.id = id || null
            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
                if(this.form.id) {
                    this.getInfo(this.form.id)
                }
            })
        },

        /**
         * 详细信息
         */
        async getInfo(id) {
            if (id) {
                const res = await this.$http({
                    url: '/mall/umsmemberlevel/info/' + id,
                    method: 'GET'
                })
                this.form = Object.assign({}, res.data)
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if(valid) {

                if(this.form.rebate == '') {
                    this.form.rebate = 0
                }
                if(this.form.commision == '') {
                    this.form.commision = 0
                }
                if(this.form.deduct == '') {
                    this.form.deduct = 0
                }
                if(this.form.distributionCommission == '') {
                    this.form.distributionCommission = 0
                }

                const res = await this.$http({
                    url: `/mall/umsmemberlevel/${!this.form.id ? 'save' : 'update'}`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        }

    }
};
</script>

<style lang="scss" scoped></style>
